﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"head.h"

int inputpassword()
{
    int count = 0;
    char password[20] = "123456";
    char input[20] = { 0 };
    for (count = 0; count < 3; count++)
    {
        printf("请输入密码：");
        scanf("%s", input);
        if (strcmp(input, password) == 0)
        {
            printf("密码正确，登录成功！\n");
            break;
        }
        else
            printf("密码错误，请重新输入！\n");
    }
    return count;
}

man* InputInformation(man* head, FILE* pf)
{
    man* tail = NULL;
    man* p = NULL;
    printf("录入信息(当编号为-1时停止输入)\n");
    while (1)
    {
        p = (man*)malloc(sizeof(man));
        if (p == NULL)
        {
            perror("malloc failed!");
            exit(1);
        }
        printf("请输入编号(禁止输入重复编号)：");
        scanf("%d", &p->flag);
        if (p->flag == -1)
        {
            break;
        }
        printf("请输入账号位置：");
        scanf("%s", p->website);
        printf("请输入账号描述：");
        scanf("%s", p->describe);
        printf("请输入用户名：");
        scanf("%s", p->name);
        printf("请输入密码：");
        scanf("%s", p->psword);

        if (head == NULL)
        {
            head = tail = p;
        }
        else
        {
            tail->next = p;
            tail = p;
        }
    }
    free(p);
    p = NULL;
    if(tail != NULL)
        tail->next = NULL;
    WriteFile(head, pf);
    printf("信息已录入！\n");
    return head;
}

void PrintInformation(man* head, FILE* pf)
{
    head = InputFile(head, pf);
    man* p = NULL;
    printf("编号    账号位置             账号描述             用户名               密码\n");
    for (p = head; p != NULL; p = p->next)
    {
        if(p != NULL)
            printf("%-7d %-20s %-20s %-20s %-20s\n", p->flag, p->website, p->describe, p->name, p->psword);
    }
}

man* AddInformation(man* head, FILE* pf)
{
    head = InputFile(head, pf);

    printf("请输入要添加的账户信息：\n");
    man* p = NULL;
    man* tmp = NULL;
    man* new = NULL;
    for (p = head; p != NULL; p = p->next)
    {
        tmp = p;
    }
    new = (man*)malloc(sizeof(man));
    if (new == NULL)
    {
        perror("malloc failed!");
        exit(1);
    }

    printf("请输入编号：");
    scanf("%d", &new->flag);
    printf("请输入账号位置：");
    scanf("%s", new->website);
    printf("请输入账号描述：");
    scanf("%s", new->describe);
    printf("请输入用户名：");
    scanf("%s", new->name);
    printf("请输入密码：");
    scanf("%s", new->psword);

    if(tmp != NULL)
        tmp->next = new;
    new->next = NULL;
    WriteFile(head, pf);
    printf("添加成功！\n");
    return head;
}

man* DelInformation(man* head, FILE* pf)
{
    head = InputFile(head, pf);
    man* p = NULL;
    man* tmp = NULL;
    printf("请输入要删除的账户编号：");
    int input = 0;
    char flag = 0;
    scanf("%d", &input);
    for (p = head; p != NULL; p = p->next)
    {
        if (p->flag == input)
        {
            printf("编号    账号位置             账号描述             用户名               密码\n");
            printf("%-7d %-20s %-20s %-20s %-20s\n", p->flag, p->website, p->describe, p->name, p->psword);
            break;
        }
        else
        {
            tmp = p;
        }
    }

    if (p != NULL)
    {
        printf("确认要删除吗？（Y/N）\n");
        //getchar();
        scanf(" %c", &flag);
        switch (flag)
        {
        case 'Y':
            if (p == head)
                head = head->next;
            else
                tmp->next = p->next;
            free(p);
            p = NULL;
            printf("已成功删除！\n");
            break;
        case 'N':
            printf("操作已撤销！\n");
            break;
        default:
            printf("无效的操作！\n");
            break;
        }
    }
    else
        printf("查无此人！\n");
    WriteFile(head, pf);
    return head;
}

void SeekInformation(man* head, FILE* pf)
{
    head = InputFile(head, pf);
    int input = 0;
    printf("请输入要查找账户的编号：");
    scanf("%d", &input);
    man* p = NULL;
    for (p = head; p != NULL; p = p->next)
    {
        if (p->flag == input)
        {
            printf("编号    账号位置             账号描述             用户名               密码\n");
            printf("%-7d %-20s %-20s %-20s %-20s\n", p->flag, p->website, p->describe, p->name, p->psword);
            break;
        }
    }
    if (p == NULL)
        printf("查无此人！\n");
}

void WriteFile(man* head, FILE* pf)
{
    pf = fopen("test.txt", "w");
    if (pf == NULL)
    {
        perror("file opening failed!");
        return;
    }

    man* p = NULL;
    for (p = head; p != NULL; p = p->next)
        fprintf(pf, "%-7d %-20s %-20s %-20s %-20s\n", p->flag, p->website, p->describe, p->name, p->psword);

    fclose(pf);
    pf = NULL;
}

man* InputFile(man* head, FILE* pf)
{
    pf = fopen("test.txt", "r");
    if (pf == NULL)
    {
        perror("file opening failed!");
        return head;
    }

    man* p = NULL;
    man* tail = NULL;

    p = (man*)malloc(sizeof(man));
    if (p == NULL)
    {
        perror("malloc failed!");
        exit(1);
    }
    while (fscanf(pf, "%d %s %s %s %s", &p->flag, p->website, p->describe, p->name, p->psword) != EOF)
    {
        if (head == NULL || tail == NULL)
            head = tail = p;
        else
        {
            tail->next = p;
            tail = p;
        }

        p = (man*)malloc(sizeof(man));
        if (p == NULL)
        {
            perror("malloc failed!");
            exit(1);
        }
    }
    tail->next = NULL;

    fclose(pf);
    pf = NULL;
    return head;
}
